package com.zero.studentsystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class LoginSystem {
    //用final修饰符 固定变量  为常量  常量不能被改变 常量命名都为大写 字母间用下划线隔开
    //固定 方法为规则    固定  类  为最终类
    private static final String LOGIN = "1";
    private static final String LOGON = "2";
    private static final String FORGET_PASSWORD = "3";
    private static final String EXIT = "4";

    //static静态代码块  随类的加载尔加载
    static ArrayList<UserDate> loginList = new ArrayList<>();

    static {
        loginList.add(new UserDate("001", "爱丽丝", "1", "千年"));
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1：登录");
            System.out.println("2：注册");
            System.out.println("3.忘记密码");
            System.out.println("请输入你的选择：");
            String chooseNumber = sc.next();
            switch (chooseNumber) {
                case LOGIN -> login(loginList);
                case LOGON -> logon(loginList);
                case FORGET_PASSWORD -> forgetPassWord(loginList);
                case EXIT -> {
                    System.out.println("谢谢使用再见");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //登录   主要功能
    private static void login(ArrayList<UserDate> loginList) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String inputName = sc.next();
            int index = index(loginList, inputName);
            if (index < 0) {
                System.out.println("用户未注册，请先注册");
                return;
            }
            System.out.println("请输入密码");
            String inputPassWord = sc.next();
            while (true) {
                String code = generateCode();
                System.out.println("正确的验证码是" + code);
                System.out.println("请输入验证码");
                String inputCode = sc.next();
                if (inputCode.equalsIgnoreCase(code)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误");
                    continue;
                }
            }
            UserDate ud = new UserDate(inputName, inputPassWord, null, null);
            boolean result = checkUserInfo(loginList, ud);
            if (result) {
                System.out.println("登录成功，欢迎使用学生管理系统");
                break;
            } else {
                System.out.println("登录失败，用户名或密码错误");
                if (i == 2) {
                    System.out.println("当前账号" + inputName + "已经被锁定，请联系管理员解锁");
                    return;
                } else {
                    System.out.println("还剩下" + (2 - i) + "次机会");
                }
            }
        }


    }

    //注册    主要功能
    private static void logon(ArrayList<UserDate> loginList) {
        Scanner sc = new Scanner(System.in);
        String inputName;
        String inputPassWord;
        String personId;
        String numberPhone;
        //用户名判断
        while (true) {
            System.out.println("请输入用户名");
            inputName = sc.next();
            boolean flag = flag1(inputName);
            if (!flag) {
                System.out.println("用户名格式错误，请再次输入");
                continue;
            }
            int index = index(loginList, inputName);
            if (index >= 0) {
                System.out.println("用户名不唯一，请再次输入");
            } else {
                System.out.println("用户名可用");
                break;
            }
        }
        //密码判断
        while (true) {
            System.out.println("请输入要注册的密码");
            inputPassWord = sc.next();
            System.out.println("请再次输入要注册的密码");
            String secondPassWord = sc.next();
            if (!inputPassWord.equals(secondPassWord)) {
                System.out.println("两次录入的密码不一致，请重新输入");
                continue;
            } else {
                System.out.println("两次录入的密码一致，请继续输入其他数据");

                break;
            }
        }
        //身份证判断
        while (true) {
            System.out.println("请输入身份证号码");
            personId = sc.next();
            boolean flag = checkPersonId(personId);
            if (!flag) {
                System.out.println("身份证格式错误请重新录入");
            } else {
                System.out.println("身份证号码录入成功");
                break;
            }
        }
        //录入手机号码
        while (true) {
            System.out.println("请输入手机号码");
            numberPhone = sc.next();
            boolean flag = checkNumberPhone(numberPhone);
            if (!flag) {
                System.out.println("手机号码格式错误，请重新输入");
                continue;
            } else {
                System.out.println("手机号码录入完毕");
                break;
            }
        }
        UserDate ud = new UserDate(inputName, inputPassWord, personId, numberPhone);
        loginList.add(ud);
        System.out.println("注册完毕");
        printList(loginList);
    }

    //忘记密码   主要功能
    private static void forgetPassWord(ArrayList<UserDate> loginList) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String inputName = sc.next();
        int index = index(loginList, inputName);
        if (index < 0) {
            System.out.println("当前账号未注册");
            return;
        }
        System.out.println("请输入身份账号");
        String inputIdNumber = sc.next();

        System.out.println("请输入手机号码");
        String inputNumberPhone = sc.next();

        UserDate ud = new UserDate(null, null, inputIdNumber, inputNumberPhone);
        int indexNumber = compareNumber(loginList, ud);
        if (indexNumber < 0) {
            System.out.println("账号信息不匹配，修改失败");
            return;
        }
        while (true) {
            System.out.println("请输入密码进行修改");
            String PassWord = sc.next();
            System.out.println("请再次输入要修改的密码");
            String newPassWord = sc.next();
            if (PassWord.equals(newPassWord)) {
                loginList.get(indexNumber).setPassWord(PassWord);
                System.out.println("密码修改成功，请重新登录");
                return;
            } else {
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }
        }
    }

    //判断身份证号  手机号码 是否正确
    private static int compareNumber(ArrayList<UserDate> loginList, UserDate ud) {
        for (int i = 0; i < loginList.size(); i++) {
            UserDate u = loginList.get(i);
            if (u.getNumberIdCard().equalsIgnoreCase(ud.getNumberIdCard()) && u.getNumberPhone().equals(ud.getNumberPhone())) {
                return i;
            }
        }
        return -1;
    }

    //判断账号 密码是否正确 ，一次判断两个，将账号数据放到对象里判断
    private static boolean checkUserInfo(ArrayList<UserDate> list, UserDate ud) {
        for (int i = 0; i < list.size(); i++) {
            UserDate user = list.get(i);
            if (user.getUserName().equals(ud.getUserName()) && user.getPassWord().equals(ud.getPassWord())) {
                return true;
            }
        }
        return false;
    }

    //生成验证码//生成验证码  长度为5  由4位字母和1个数字组成 数字出现在任意位置
    private static String generateCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }

        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int number = r.nextInt(10);
        sb.append(number);

        char[] chs = sb.toString().toCharArray();
        int randomIndex = r.nextInt(chs.length);
        char temp = chs[randomIndex];
        chs[randomIndex] = chs[chs.length - 1];
        chs[chs.length - 1] = temp;
        String getCode = new String(chs);
        return getCode;

        /*char[] code = new char[5];
        char[] arr = new char[62];
        int a = 97;
        int A = 65;
        int zero = 48;
        for (int i = 0; i < arr.length; i++) {
            if (i < 26) {
                arr[i] = (char) a;
                a++;
            } else if (26 <= i && i < 52) {
                arr[i] = (char) A;
                A++;
            } else {
                arr[i] = (char) zero;
                zero++;
            }
        }
        Random r = new Random();
        for (int i = 0; i < code.length; i++) {
            int index = code.length - 1;
            if (i == index) {
                int number = r.nextInt(11) + 52;
                code[i] = arr[number];
            } else {
                int abNumber = r.nextInt(52);
                code[i] = arr[abNumber];
            }
        }
        for (int i = 0; i < code.length; i++) {
            int ran = r.nextInt(code.length);
            char c = code[i];
            code[i] = code[ran];
            code[ran] = c;
        }
        String result = new String(code);
        return result;*/

    }

    //遍历集合  打印注册信息
    private static void printList(ArrayList<UserDate> loginList) {
        for (int i = 0; i < loginList.size(); i++) {
            UserDate ud = loginList.get(i);
            System.out.println(ud.getUserName() + "," + ud.getPassWord() + "," + ud.getNumberIdCard() + "," + ud.getNumberPhone());
        }
    }

    //判断手机号码格式是否符合要求
    private static boolean checkNumberPhone(String numberPhone) {
        if (numberPhone.length() != 11) {
            System.out.println("手机号码长度不够11位");
            return false;
        }
        if (numberPhone.startsWith("0")) {
            System.out.println("手机号码第一位不能为0");
            return false;
        }
        for (int i = 0; i < numberPhone.length(); i++) {
            char c = numberPhone.charAt(i);
            if (!('0' <= c && c <= '9')) {
                System.out.println("手机号码必须是纯数字");
                return false;
            }
        }
        return true;
    }

    //判断身份证格式是否符合要求
    private static boolean checkPersonId(String personId) {
        int len = personId.length();
        if (len != 18) {
            System.out.println("号码长度不符合");
            return false;
        }
        if (personId.startsWith("0")) {
            System.out.println("号码第一位不能为0");
            return false;
        }
        for (int i = 0; i < personId.length() - 1; i++) {
            char c = personId.charAt(i);
            if (!(48 <= c && 57 >= c)) {
                System.out.println("前17位必须是数字");
                return false;
            }
        }
        char end = personId.charAt(personId.length() - 1);
        if (!((48 <= end && 57 >= end) || end == 83 || end == 120)) {
            System.out.println("身份证最后一位错误");
            return false;
        } else {
            return true;
        }


    }

    //判断用户名是否符合要求
    private static boolean flag1(String inputName) {
        //判断长度
        int len = inputName.length();
        if (3 > len || len > 15) {
            System.out.println("用户名长度不符合");
            return false;
        }
        //判断是否是数字和字母
        for (int i = 0; i < inputName.length(); i++) {
            char c = inputName.charAt(i);
            if (!((48 <= c && 58 >= c) || (65 <= c && 90 >= c) || (97 <= c && 122 >= c))) {
                System.out.println("用户名必须字母加数字");
                return false;
            }
        }
        //判断是否为纯数字
        int count = 0;
        for (int i = 0; i < inputName.length(); i++) {
            char c = inputName.charAt(i);
            if ((65 <= c && 90 >= c) || (97 <= c && 122 >= c)) {
                count++;
                break;
            }
        }

        return count > 0;


    }

    //判断用户名是否唯一
    public static int index(ArrayList<UserDate> loginList, String inputName) {
        for (int i = 0; i < loginList.size(); i++) {
            UserDate ud = loginList.get(i);
            String userName = ud.getUserName();
            if (userName.equals(inputName)) {
                return i;
            }
        }
        return -1;
    }

}


